﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.Timers;

namespace LuctecWS.Core
{
    public class SendMail
    {
        private static System.Timers.Timer _Timer;
        private static bool _Started;
        private static bool _Created;
        private static List<ListSendEmail> _ListSendMail;

        public static void Create()
        {
            if (!_Created)
            {
                _Created = true;

                _Timer = new System.Timers.Timer();
                _Timer.Interval = Convert.ToInt32(ConfigurationManager.AppSettings["TimerMonitoring_Minute"]) * 60000;
                _Timer.Elapsed += Timer_Elapsed;

                _ListSendMail = new List<ListSendEmail>();

                _ListSendMail.Add(Get());
                _ListSendMail.Add(Get());

                Start();
            }
        }

        public static void Start()
        {
            _Timer.Start();
        }

        public static void Stop()
        {
            _Timer.Stop();
        }

        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (_ListSendMail)
            {
                foreach (var lsm in _ListSendMail)
                {
                    Send(lsm);
                }

                //SaveDB

                _ListSendMail.Clear();

                //ReadDB

                _ListSendMail.Add(Get());
            }
        }

        private static void Send(ListSendEmail lsm)
        {
            foreach (var to in lsm.Tos)
            {
                try
                {
                    MailMessage objEmail = new MailMessage();
                    objEmail.To.Add(to.EmailAddress);
                    objEmail.From = new MailAddress(ConfigurationManager.AppSettings["From"]);
                    objEmail.Subject = lsm.Template.Name;
                    objEmail.Body = lsm.Template.Body;

                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = ConfigurationManager.AppSettings["HostAddress"];
                    smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["From"], ConfigurationManager.AppSettings["Password"]);
                    //smtp.EnableSsl = true;
                    //smtp.Send(objEmail);

                    to.Status = EmailStatus.Sended;
                }
                catch (Exception ex)
                {
                    to.Status = EmailStatus.Error;
                }
            }
        }

        private static ListSendEmail Get()
        {
            ListSendEmail lsm = new ListSendEmail();

            List<To> tos = new List<To>()
                {
                    new To(){ Status = EmailStatus.ToSend, EmailAddress = "cereso_rodrigues@gmail.com" },
                    new To(){ Status = EmailStatus.ToSend, EmailAddress = "cereso_rodrigues@gmail.com" },
                    new To(){ Status = EmailStatus.ToSend, EmailAddress = "cereso_rodrigues@gmail.com" },
                    new To(){ Status = EmailStatus.ToSend, EmailAddress = "cereso_rodrigues@gmail.com" },
                    new To(){ Status = EmailStatus.ToSend, EmailAddress = "cereso_rodrigues@gmail.com" }
                };

            lsm.Tos = tos;
            lsm.Template = new EmailTemplate();

            return lsm;
        }
    }
}